import MyIcon from '@/components/MyIcon';
import { tooltipLength } from '@/utils';
import pageStyles from '@/common/pageLayout.less';
import { Button, InputNumber, Tooltip } from 'antd';
import { DeleteOutlined, PlusCircleOutlined } from '@ant-design/icons';
import { ColumnsRenderFun, DictionaryRenderFun, formatDecimal, getColorTextDoms, getSorter } from '../../Acommon';

/**
 * @getPackTableColumns 采购申请第二步表格配置
 * @param onRemove 删除标的
 * @param changeTargetInfo 选择标的
 * @returns
 */
export const getPackTableColumns = ({ onRemove, onSelectProject, selectModal, finallyType, applyInfo }: Record<string, any>): Record<string, any>[] => {
  let colums = [
    {
      title: '标的名称',
      dataIndex: 'fullName',
      key: 'fullName',
      width: 200,
      visible: true,
      fixed: 'left',
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    // {
    //   title: '标的类别',
    //   dataIndex: 'supplyTypeName',
    //   key: 'supplyTypeName',
    //   width: 200,
    //   visible: true,
    //   render: (text: string, record) => DictionaryRenderFun({ text, isTooltip: true }),
    // },
    // {
    //   title: '规格要求（参数）',
    //   dataIndex: 'needParams',
    //   key: 'needParams',
    //   width: 150,
    //   visible: true,
    //   render: (text: string) => ColumnsRenderFun({ text, isTooltip: true, textStyle: { width: '80%' } }),
    // },
    {
      title: '规格要求（参数）',
      dataIndex: 'needParams',
      key: 'needParams',
      width: 300,
      visible: true,
      // fixed: 'left',
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true, wordNum: 200 }),
    },
    {
      title: '项目编号',
      dataIndex: 'projectCode',
      key: 'projectCode',
      width: 200,
      render: (text, record) => {
        if (applyInfo?.id && applyInfo?.supplyWay == 1) {
          return (
            <Tooltip title={text}>
              <div className={pageStyles['over-flow-hidden']}>
                {finallyType != 'view' && (
                  <a style={{ padding: '0 5px' }}>
                    <PlusCircleOutlined onClick={() => onSelectProject(record)} />
                  </a>
                )}
                {text || '--'}
              </div>
            </Tooltip>
          );
        } else {
          return (
            <Tooltip title={text}>
              <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
            </Tooltip>
          );
        }
      },
    },
    {
      title: '项目名称',
      dataIndex: 'projectName',
      key: 'projectName',
      width: 200,
      render: (text, record) => {
        // if (applyInfo?.id && applyInfo?.supplyWay == 1) {
        // 零星
        // return <div>1</div>
        // } else {
        return (
          <Tooltip title={text}>
            <div className={pageStyles['over-flow-hidden']}>{text || '--'}</div>
          </Tooltip>
        );
        // }
      },
    },
    {
      title: '项目金额（元）',
      dataIndex: 'projectSum',
      key: 'projectSum',
      width: 200,
      render: (text, record) => {
        // if (applyInfo?.id && applyInfo?.supplyWay == 1) {
        // 零星
        // return <div>1</div>
        // } else {
        return <div className={pageStyles['over-flow-hidden']}>{formatDecimal(text) || '--'}</div>;
        // }
      },
    },
    {
      title: '物料类型',
      dataIndex: 'materialTypeName',
      key: 'materialTypeName',
      width: 200,
      visible: true,
      render: (text: string, record) => DictionaryRenderFun({ text, isTooltip: true }),
    },
    {
      title: '预估单价（元）',
      dataIndex: 'expectPrice',
      key: 'expectPrice',
      width: 200,
      visible: true,
      render: (text: number) => Number(text).toFixed(2),
    },
    {
      title: '数量（单位）',
      dataIndex: 'expectNum',
      key: 'expectNum',
      width: 200,
      visible: true,
      render: (text: string, record: Record<string, any>) => {
        return (
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
            <span>{text}</span>
            <span>{getColorTextDoms(record.unitBusinessName)}</span>
          </div>
        );
      },
    },
    {
      title: '预估总价（元）',
      dataIndex: 'expectSum',
      key: 'expectSum',
      width: 200,
      visible: true,
      render: (text: number) => {
        return Number(text).toFixed(2);
      },
    },
    {
      title: (
        <Button
          type={'link'}
          size={'small'}
          icon={<PlusCircleOutlined />}
          className="manageBtntTxtStyle"
          onClick={() => {
            selectModal({
              packInfo: {},
              isShow: true,
            });
          }}
        >
          选择
        </Button>
      ),
      dataIndex: 'operation',
      key: 'operation',
      width: 200,
      visible: true,
      unDrag: true,
      align: 'center',
      fixed: 'right',
      render: (_: string, record: Record<string, any>, index: number) => {
        return (
          <Button
            danger
            type={'link'}
            size={'small'}
            className="manageBtntTxtStyle"
            onClick={() => onRemove(record.id, index, record.fullName ?? '标一')}
            icon={<DeleteOutlined />}
          >
            删除
          </Button>
        );
      },
    },
  ];

  if (finallyType == 'view' || !applyInfo?.id) {
    colums = colums?.filter((item) => item?.key != 'operation');
  }

  return colums;
};

/**
 * @getApplyPackTableColumns 采购申请第三步表格配置
 * @param dataSource 表格数据
 * @param jumpSource 跳转寻源
 * @returns
 */
export const getApplyPackTableColumns = ({ onView }: Record<string, any>): Record<string, any>[] => {
  return [
    {
      title: '包号',
      dataIndex: 'packCode',
      key: 'packCode',
      width: 80,
      visible: true,
      render: (text: string) => {
        return ColumnsRenderFun({ text });
      },
    },
    {
      title: '包名称',
      dataIndex: 'packName',
      key: 'packName',
      width: 200,
      visible: true,
      render: (text: string, record: Record<string, any>) => {
        return (
          <a
            onClick={() => {
              onView(record);
            }}
            style={{ color: '100%' }}
          >
            <Tooltip title={tooltipLength(text, 50)}>
              <span className={pageStyles['over-flow-hidden']}>{text || '--'}</span>
            </Tooltip>
          </a>
        );
      },
    },
    {
      title: '预算金额（元）',
      dataIndex: 'expectSum',
      key: 'expectSum',
      width: 150,
      visible: true,
      render: (text: string) => {
        // return Number(text).toFixed(2);
        return formatDecimal(text);
      },
    },
    {
      title: '采购组织形式',
      dataIndex: 'procureOrgName',
      key: 'procureOrgName',
      width: 150,
      visible: true,
      render: (text: string, record: Record<string, any>, index: number) => {
        // return mergeCells(dataSource, ['packName', 'procureRealName'], text, record, index);
        return DictionaryRenderFun({ text });
      },
    },
    {
      title: '拟采用采购方式',
      dataIndex: 'procureRealName',
      key: 'procureRealName',
      width: 150,
      visible: true,
      render: (text: string, record: Record<string, any>, index: number) => {
        // return mergeCells(dataSource, ['packName', 'procureRealName'], text, record, index);
        return DictionaryRenderFun({ text });
      },
    },
    // {
    //   title: '标包状态',
    //   dataIndex: 'finishState',
    //   key: 'finishState',
    //   width: 150,
    //   render: (text) => {
    //     if (!text || text == 0) {
    //       return <MyIcon style={{ fontSize: 40 }} type={'icon-weiwancheng'} />;
    //     }
    //     if (text == 1) {
    //       return <MyIcon style={{ fontSize: 40 }} type={'icon-yiwancheng'} />;
    //     }
    //   },
    // },
  ];
};

/**
 * @getMaterialTableColumns 步骤条第二步 弹窗中标的列表配置
 * @param onEditTable 表格编辑事件
 * @returns
 */
export const getMaterialTableColumns = ({ onEditTable, setisNextBtnFun }: any): Record<string, any>[] => {
  return [
    {
      title: '标的名称',
      dataIndex: 'fullName',
      key: 'fullName',
      width: 150,
      visible: true,
      fixed: 'left',
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '计划名称',
      dataIndex: 'planName',
      key: 'planName',
      width: 250,
      visible: true,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    // {
    //   title: '标的类型',
    //   dataIndex: 'supplyTypeName',
    //   key: 'supplyTypeName',
    //   width: 140,
    //   visible: true,
    //   render: (text: string, record) => DictionaryRenderFun({ text }),
    // },
    {
      title: '规格要求（参数）',
      dataIndex: 'needParams',
      key: 'needParams',
      width: 150,
      visible: true,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '标包号',
      dataIndex: 'packCode',
      key: 'packCode',
      width: 150,
      render: (text: string, record) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '项目编号',
      dataIndex: 'projectCode',
      key: 'projectCode',
      width: 200,
      render: (text, record) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '项目名称',
      dataIndex: 'projectName',
      key: 'projectName',
      width: 200,
      render: (text, record) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '项目金额（元）',
      dataIndex: 'projectSum',
      key: 'projectSum',
      width: 200,
      render: (text, record) => {
        return <div className={pageStyles['over-flow-hidden']}>{formatDecimal(text) || '--'}</div>;
      },
    },
    {
      title: '计划数量（单位）',
      dataIndex: 'expectNum',
      key: 'expectNum',
      width: 165,
      visible: true,
      render: (text: string, record: Record<string, any>) => {
        return (
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
            <span>{text || 0}</span>
            {getColorTextDoms(record?.unitBusinessName)}
          </div>
        );
      },
    },
    {
      title: '已采数量（单位）',
      dataIndex: 'completeNum',
      key: 'completeNum',
      width: 165,
      visible: true,
      render: (text: string, record: Record<string, any>) => {
        return (
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
            <span>{text || 0}</span>
            {getColorTextDoms(record?.unitBusinessName)}
          </div>
        );
      },
    },
    {
      title: '锁定数量（单位）',
      dataIndex: 'lockNum',
      key: 'lockNum',
      width: 165,
      visible: true,
      render: (text: string, record: Record<string, any>) => {
        return (
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
            <span>{text || 0}</span>
            {getColorTextDoms(record?.unitBusinessName)}
          </div>
        );
      },
    },
    {
      title: '预估单价（元）',
      dataIndex: 'expectPrice',
      key: 'expectPrice',
      width: 160,
      visible: true,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: '预估总价（元）',
      dataIndex: 'expectSum',
      key: 'expectSum',
      width: 160,
      visible: true,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    {
      title: (
        <>
          <MyIcon type="icon-edit1" style={{ marginRight: '.5rem' }} />
          采购数量
        </>
      ),
      dataIndex: 'inpNum',
      key: 'inpNum',
      width: 180,
      fixed: 'right',
      render: (text: number | string, record: Record<string, any>) => {
        const MaxNum: number = Number(record?.prepareNum || 0) - Number(record?.lockNum || 0);
        // 控制当数量大于当前可选数量时显示
        let ishowMax = Number(text) > MaxNum ? true : false;
        let ishowMin = Number(text) <= 0;
        if (MaxNum <= 0) {
          return <div style={{ textAlign: 'center', padding: '0 11px' }}>暂无采购数量</div>;
        } else {
          return (
            <>
              <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
                <InputNumber
                  status={ishowMax || ishowMin ? 'error' : ''}
                  bordered={ishowMax || ishowMin ? true : false}
                  value={text}
                  style={{ width: '100%' }}
                  onChange={(value: any) => {
                    if (Number(value) > MaxNum || Number(value) <= 0) {
                      setisNextBtnFun(true);
                    } else {
                      setisNextBtnFun(false);
                    }
                    onEditTable({
                      value: value,
                      key: 'prepareNum',
                      type: 'InputNumber',
                      record: record,
                    });
                  }}
                  min={1}
                  // max={MaxNum}
                  disabled={text == 0}
                />
                {getColorTextDoms(record?.unitBusinessName)}
              </div>
              {ishowMax && <div style={{ paddingLeft: '5px', color: 'red' }}>{`最多可选择的数量为${MaxNum}`}</div>}
              {ishowMin && <div style={{ paddingLeft: '5px', color: 'red' }}>{`最少选择的数量为1`}</div>}
            </>
          );
        }
      },
    },
  ];
};

/**
 * @getProjectTableColumns 步骤条第一步 项目弹窗配置
 */
export const getProjectTableColumns = (): Record<string, any>[] => {
  return [
    // {
    //     title: '编号',
    //     dataIndex: 'id',
    //     key: 'id',
    //     visible: false,
    //     width: 150,
    // },
    {
      title: '项目编号',
      dataIndex: 'projectCode',
      key: 'projectCode',
      visible: true,
      width: 140,
      sorter: (a: any, b: any) => getSorter(a?.projectCode, b?.projectCode),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '项目名称',
      dataIndex: 'projectName',
      key: 'projectName',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => getSorter(a?.projectName, b?.projectName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    // {
    //   title: '部门名称',
    //   dataIndex: 'bm',
    //   key: 'bm',
    //   visible: true,
    //   width: 150,
    //   sorter: (a: any, b: any) => getSorter(a?.bm, b?.bm),
    //   render: (text: string) => ColumnsRenderFun({ text }),
    // },
    {
      title: '项目费用（元）',
      dataIndex: 'projectSum',
      key: 'projectSum',
      visible: true,
      width: 120,
      sorter: (a: any, b: any) => getSorter(a?.projectSum, b?.projectSum),
      render: (text: string) => formatDecimal(text),
    },
    // {
    //   title: '项目描述',
    //   dataIndex: 'projectIntro',
    //   key: 'projectIntro',
    //   visible: true,
    //   width: 260,
    //   sorter: (a: any, b: any) => getSorter(a?.projectIntro, b?.projectIntro),
    //   render: (text: string) => ColumnsRenderFun({ text }),
    // },
  ];
};
